package com.ejuornal.action;
import java.util.List;

import javax.annotation.Resource;

import com.ejuornal.bean.Category;
import com.ejuornal.bean.PerPeriodical;
import com.ejuornal.bean.Periodical;
import com.ejuornal.bean.Subscribe;
import com.ejuornal.bean.vo.UserSubscibeJudge;
import com.ejuornal.service.CategoryService;
import com.ejuornal.service.PerPeriodicalService;
import com.ejuornal.service.PeriodicalService;
import com.ejuornal.service.SubscribeService;
import com.ejuornal.service.UserSJService;
import com.ejuornal.service.UserService;

public class SubscribeBAction extends BaseAction<UserSubscibeJudge> {
	private static final long serialVersionUID = 1L;
	private PeriodicalService periodicalService;
	private SubscribeService subService;
	private UserSJService userSJService;
	private UserService userService;
	private CategoryService categoryService;
    private PerPeriodicalService perPeriodicalService;
    private Subscribe subscribe;
	private Periodical periodical;
	private int categoryId;
	private List<Category> categorys;
	private PerPeriodical perPeriodical;
	private String value;
	private String perPeriodicalId;
	
	/**
	 * 订阅一个期刊
	 * @return
	 */
	public String addSubscribe(){
		int userId = getUserIdBySession();
		//如果此条件为真，说明此期刊未订阅；如果为假，说明已经订阅，直接返回页面
		if(0 == subService.isSubscribe(userId, periodical.getPeriodicalId())) {
			subscribe = new Subscribe();
			subscribe.setUserId(userId);
			subscribe.setPeriodicalId(periodical.getPeriodicalId());
			categoryId = periodicalService.findById(periodical.getPeriodicalId()).getCategoryId();   
			subService.add(subscribe);
		}
		
		if("2".equals(value)) {
			return "periodical";
		}
		
		return SUCCESS;
	}
	
	public String  findNewPerPeriodicalByPerPeriodicalId(){
		perPeriodical = perPeriodicalService.findNewPerPeriodical(periodical.getPeriodicalId(), "1");
		return  SUCCESS;
	}
	/**@author lyy
	 * 根据用户是否登录  得到期刊列表
	 * @return
	 */
	public String findAllJudge(){
	   // categoryId = 3;
		String  url = "subscribeB/findAllJudge?categoryId=" + categoryId + "&";
		categorys = categoryService.findAll();
		int userId = getUserIdBySession();
		if(userId == -1){
			pages =userSJService.findAllJudge((page- 1)*10, url, categoryId);
		}else {
			pages = userSJService.findAllJudge((page - 1)*10, url, categoryId, userId);
		}
		return SUCCESS;
	}
	/**
	 * 废弃
	 * @author lcy
	 * 根据期刊编号 periodicalId 超找一个期刊信息   
	 * @return
	 */
	public String  findByperiodicalId(){
		periodical = periodicalService.findById(periodical.getPeriodicalId());
		return SUCCESS;
	}
	/**
	 * 空间用的
	 * @return
	 */
	public String  findSUJByuserId(){
		 String url = "subscribeB/findSUJByuserId";
		 int userId = getUserIdBySession();
		 pages = userSJService.findSUJByuserId((page - 1)*10, url, userId);
		 return  SUCCESS;
	 }
	 
	public String  cancelSubscribe(){
		int userId = getUserIdBySession();
		subService.cancelSubscribe(periodical.getPeriodicalId(), userId);
		return  SUCCESS;
	}
	

	 public String getPerPeriodicalId() {
		return perPeriodicalId;
	}

	public void setPerPeriodicalId(String perPeriodicalId) {
		this.perPeriodicalId = perPeriodicalId;
	}

	public PeriodicalService getPeriodicalService() {
		return periodicalService;
	}
	 
	 @Resource(name="periodicalService")
	public void setPeriodicalService(PeriodicalService periodicalService) {
		this.periodicalService = periodicalService;
	}

	
	public PerPeriodicalService getPerPeriodicalService() {
		return perPeriodicalService;
	}
	 @Resource(name="perPeriodicalService")
	public void setPerPeriodicalService(PerPeriodicalService perPeriodicalService) {
		this.perPeriodicalService = perPeriodicalService;
	}

	public SubscribeService getSubService() {
		return subService;
	}
	@Resource(name="subscribeService")
	public void setSubService(SubscribeService subService) {
		this.subService = subService;
	}

	
	public UserSJService getUserSJService() {
		return userSJService;
	}
	@Resource(name="userSJServiceImpl")
	public void setUserSJService(UserSJService userSJService) {
		this.userSJService = userSJService;
	}
	public UserService getUserService() {
		return userService;
	}
	
	@Resource(name="userService")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}
	@Resource(name="categoryService")
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	
	public Subscribe getSubscribe() {
		return subscribe;
	}

	public void setSubscribe(Subscribe subscribe) {
		this.subscribe = subscribe;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public int getCategoryId() {
		return categoryId;
	}
	public void setCategoryId(int categoryId) {
		this.categoryId = categoryId;
	}
	public Periodical getPeriodical() {
		return periodical;
	}
	public void setPeriodical(Periodical periodical) {
		this.periodical = periodical;
	}
	public List<Category> getCategorys() {
		return categorys;
	}
	public void setCategorys(List<Category> categorys) {
		this.categorys = categorys;
	}
	public PerPeriodical getPerPeriodical() {
		return perPeriodical;
	}
	public void setPerPeriodical(PerPeriodical perPeriodical) {
		this.perPeriodical = perPeriodical;
	}
	/*public Subscribe getSubscribe() {
		return subscribe;
	}
	public void setSubscribe(Subscribe subscribe) {
		this.subscribe = subscribe;
	}*/
	
}
